﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ClassBuilder.Comment
{
    /// <summary>
    /// 示例（注释）
    /// </summary>
    public class Example
    {

        /// <summary>
        /// 示例
        /// </summary>
        /// <param name="code">示例代码</param>
        /// <param name="desc">示例描述</param>
        public Example(string code = null, string desc = null)
        {
            this.Code = code;
            this.Desc = desc;
        }

        public override string ToString()
        {
            if (Code.NotEmpty() || Desc.NotEmpty())
            {
                StringPlus plus = new StringPlus();
                plus.AppendLine("/// <example>");

                if (Desc.NotEmpty())
                {
                    plus.AppendLine($"/// {Desc}");
                }

                if (Code.NotEmpty())
                {
                    plus.AppendLine("/// <code>");
                    var lines = Code.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var line in lines)
                    {
                        plus.AppendLine($"/// {line.HtmlHandle()}");
                    }
                    plus.AppendLine("/// </code>");
                }

                plus.AppendLine("/// </example>");
                return plus.ToString().Trim();
            }
            return string.Empty;
        }


        /// <summary>
        /// 所有行注释
        /// </summary>
        public string[] Lines
        {
            get
            {
                return this.Value?.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
            }
        }

        /// <summary>
        /// 根据tab处理tab间隔
        /// </summary>
        /// <param name="tab_space">tab间隔个数</param>
        /// <returns></returns>
        public string ByTabs(int tab_space)
        {
            StringPlus plus = new StringPlus();
            foreach (var line in Lines)
            {
                plus.AppendSpaceLine(tab_space, line);
            }
            return plus.ToString();
        }


        /// <summary>
        /// 代码内容
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Desc { get; set; }

        /// <summary>
        /// 最终显示内容
        /// </summary>
        public string Value
        {
            get
            {
                return this.ToString();
            }
        }
    }
}
